//
//  PlotsListTableViewController.m
//  PlotMate
//
//  Created by Brian Boyle on 05/04/2012.
//  Copyright (c) 2012 Brian Boyle. All rights reserved.
//

#import "PlotsListTableViewController.h"
#import "AppDelegate.h"
#import "Plot.h"
#import "BedsListTableViewController.h"
#import "UITableView+TableSectionHeaderText.h"
#import "UILabel+TextFont.h"

@interface PlotsListTableViewController()
@property (nonatomic, strong) NSManagedObjectContext *context;
@end


@implementation PlotsListTableViewController
@synthesize context = _context;

/*Set the NSFetchedResultsController for the list of plots*/
- (void)setupFetchedResultsController {
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Plot"];
    NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:sorter];
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil];
}

#pragma mark - View lifecycle

-(void)viewDidLoad
{
    //Get the managedObjectContext from the AppDelegate
    self.context = [(AppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];
    
    //Set the background image
    UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"plot_bg.png"]];
    [tempImageView setFrame:self.tableView.frame]; 
    self.tableView.backgroundView = tempImageView;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self setupFetchedResultsController];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table View Delegates

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"textWithBugCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    Plot *plot = [self.fetchedResultsController objectAtIndexPath:indexPath];
    
    UILabel *label = (UILabel *)[cell viewWithTag:1];
    label.text = plot.name;
    [label setSketchBlockFont];
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    return @"Your Plots";
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self performSegueWithIdentifier:@"viewBedListSegue" sender:indexPath];
    
}

/*Custom section header view*/
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSString *sectionTitle = [self tableView:self.tableView titleForHeaderInSection:section]; 
    return [UITableView createSectionHeaderTextLabel:sectionTitle];
}

/*Method that overrides CoreDataTableViewController to change the section in the indexPath. This is needed because
 we have more than one section on a screen*/
-(NSIndexPath *)changeSectionInIndexPath:(NSIndexPath *)newIndexPath forObjectIfRequired:(id)anObject
{
    if(newIndexPath == nil){
        //PlantSet is in sesction 1, so we want to tell the tableview that any updates to that object should go into section 1.
        //This means that section 0 will remain static and it wont check the number of rows in that section after an update.
        return [NSIndexPath indexPathForRow:0 inSection:0];
    }
    return newIndexPath;
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"viewBedListSegue"]){
        Plot *plot = [self.fetchedResultsController objectAtIndexPath:sender];
        [segue.destinationViewController setPlot:plot];
    }
}

@end
